// 事件的函数
var arrTotal = [];
var arrType = ['错误','警告','信息'];
var pages = 1; //表示页数
var totalPages = 0;
var sign = 1; //记录当前第几个块，比如我规定最多显示10个块，那么sign的取值范围就是1-10
var b_sign = 1; //记录sign变动之前的sign数值
var count;

// var real_count=count<=5?count:5;//定义最多有多少块
var real_count=count;//定义最多有多少块
var real_change = 0;
var o = 0;
  if (real_count % 2 == 0) {
    real_change = real_count / 2;
  } else {
    real_change = real_count / 2 + 0.5;
    var o = 1;
  }
  //初始化块
  let i = 1;
  while (i <= real_count) {
    var val = "<div class='num' pageNum='" + i + "' id=" + i + ">" + i + "</div>";
    $("#number").append(val)
    i++;
  }
    function getTotalPages(){
      return totalPages = arrTotal.length
    }
    // //表示每页显示几条数据
    function getListLength(){
      return getTotalPages() < 20 ? getTotalPages() : 20
    }
    //记录总共页数
    function getPagesCount(){
        return count = Math.ceil(getTotalPages() / getListLength())
    }
    function query_all() { //查找id='number'块的所有子节点
      nodes = $("#number").find("div");
      length = nodes.length;
    }
    function currentPage() { //获取当前page
      var useCur = "#" + sign;
      debugger
      if(count == 1){
        $("#before").css({
          "visibility": "hidden"
        });
        $("#next").css({
          "visibility": "hidden"
        });
        $("#last").css({
          "visibility": "hidden"
        });
      }
      // $(".current").text("当前" + $(useCur).text() + "/" + count + "页");
      $(".current").text("当前" + sign + "/" + count + "页");
      $("#taTalDataNumber").text(getTotalPages());

      var otbod = document.getElementById('table').tBodies[0]
      var s = otbod.rows.length;
      for (i = 0; i < s; i++) {
        otbod.removeChild(otbod.rows[0])
      }
      pages = sign;
      var pageIndex = (pages - 1) * getListLength() //页数-1 乘以每页现实多少条 结果为 本页从第几条开始
      // 在最后一页时对余数循环
      for (var i = 0; i < getListLength(); i++) {
        var otr = document.createElement('tr');
        var otd1 = document.createElement('td');
        var otd2 = document.createElement('td');
        var otd3 = document.createElement('td');
        var otd4 = document.createElement('td');
        otd1.innerHTML = arrTotal[pageIndex]['time'];
        otd2.innerHTML = arrType[arrTotal[pageIndex]['level']];
        otd3.innerHTML = arrTotal[pageIndex]['module'];
        otd4.innerHTML = arrTotal[pageIndex]['message'];
        otr.appendChild(otd1);
        otr.appendChild(otd2);
        otr.appendChild(otd3);
        otr.appendChild(otd4);
        otbod.appendChild(otr);
        $(otr).css({
          "color": "#000066"
        });
        if (pageIndex == getTotalPages()-1) {
          break
        }
        pageIndex++
      }
    }
    // currentPage();
    var b_change = function () { //将当前第sign块样式还原
      b_sign = sign;
      var useBchange = "#" + b_sign;
      $(useBchange).css({
        "border": "1px solid #e6e6e6",
        "color": "#666"
      });
    }
    var blue_change = function (c) { //将当前sign块样式改变，好区别于其他块
      var useBlueChange = "#" + c;
      $(useBlueChange).css({
        "border": "none",
        "color": "#3e89fa"
      });
    }
    // blue_change(sign); //页面第一次加载的时候，改变指定块样式

    function go_back(now_sign, kit) { //当前sign块先还原样式,再改变sign,再改变改变后的sign块样式
      b_change();
      if (kit == 0) {
        sign = now_sign;
      } else if (kit == 1) {
        if (sign != real_count)
          sign++;
      } else if (kit == 2) {
        if (sign != 1)
          sign--;
      }
      blue_change(sign);
    }

    function change_add(c_add, kit) { //遍历改变id='number'的子节点内容
      let p = c_add;
      for (let j = 0; j < length; j++) {
        if (kit == 0) {
          nodes[j].innerHTML = p;
          p++;
        } else if (kit == 1) {
          nodes[j].innerHTML = p;
          p--;
        } else if (kit == 2) {
          nodes[j].innerHTML = parseInt(nodes[j].innerHTML) + 1;
        } else if (kit == 3) {
          nodes[j].innerHTML = parseInt(nodes[j].innerHTML) - 1;
        }
      }
    }
    // 点击跳转至某一页的函数
    function for_num(use, id) {
      let error_1 = "#" + real_change;
      //页面在最前或最后
      if ($(error_1).text() == real_change || $(error_1).text() == count - real_change + 1) {
        //判断点击的第一个一半的前面还是点击的第二个一半的后面
        if ($(use).text() <= real_change || $(use).text() > count - real_change) {
          go_back(id, 0);
        } else {
          go_back(real_change, 0);
          change_add($(use).text() - (real_change - 1), 0);
        }
        currentPage();
      } else {
        var inner = (o == 0) ? $(use).text() >= count - real_change : $(use).text() > count - real_change;
        if ($(use).text() <= real_change || inner) {
          if ($(use).text() <= real_change) {
            go_back($(use).text(), 0);
            change_add(1, 0);
          } else {
            go_back(real_count - (count - $(use).text()), 0);
            change_add(count - real_count + 1, 0);
          }
        } else {
          go_back(real_change, 0);
          change_add($(use).text() - (real_change - 1), 0);
        }
      }
      currentPage();
    }
  